// pages/mine/makeTopic/makeTopic.js

let {
  api_AddTopic
} = require("../../api/getData.js")

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    images: [],
    image: "",
    title: "",
    content: "",
    tempFilePaths:[],
    topicid: "",
    disable:false,
    imageMax:4,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  chooseImage(e) {
    var max = this.data.imageMax;
    wx.chooseImage({
      count: max,
      sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        console.log('添加照片成功')
        // const images = this.data.images.concat(res.tempFilePaths)
        // this.data.image = res.tempFilePaths[0];
        // // 限制最多只能留下3张照片
        // this.data.images = images.length <= 3 ? images : images.slice(0, 3)
        var images = this.data.tempFilePaths.concat(res.tempFilePaths);
        if (images.length <= max){
          wx.showToast({
            title: '图片添加成功',
            duration: 2000
          })
        }else{
          wx.showToast({
            title: '已经达到上限',
            duration: 2000
          })
        }
        images = images.length <= max ? images : images.slice(0, max)
        this.setData({
          tempFilePaths:images
        })
        this.checkPic(this.data.tempFilePaths[0]);
      }
    })
  },

  setTitle: function (e) {
    this.setData({
      title: e.detail.value
    })
  },

  setContent: function (e) {
    this.setData({
      content: e.detail.value
    })
  },

  handleImagePreview(e) {
    const idx = e.target.dataset.idx
    const images = this.data.images

    wx.previewImage({
      current: images[idx],
      urls: images,
    })
  },

  removeImage:function(e) {
    var idx = e.target.dataset.idx;
    console.log("删除的为"+idx)
    var tmp = this.data.tempFilePaths;
    tmp.splice(idx, 1)
    this.setData({
      tempFilePaths:tmp,
    })
  },

  uploadTopic(){
    if(this.data.disable==true)
      return;
    this.setData({
      disable:true,
    })
    var tieziData= {
      'userid': app.globalData.userInfo.id,
      'title': this.data.title,
      'content': this.data.content,
      'piclen': this.data.tempFilePaths.length,
    }
    api_AddTopic(tieziData, (res) => {
      if(res.status==200){
        if(this.data.tempFilePaths.length==0){
          wx.showToast({
            title: '发表成功',
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/mine/topic/topic'
            })
          }, 2000) 
        }else{
          this.setData({
            topicid:res.data,
          })
          var i=this.data.tempFilePaths.length-1;
          console.log('照片数组长度:'+i)
          this.uploadimg(i);
        }
      }else{
        wx.showToast({
          title: '发表失败',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/mine/topic/topic'
          })
        }, 2000) 
      }
    })
  },
 
  uploadimg:function (i){
    var that = this;
    wx.uploadFile({
      url: app.globalData.normal_url+'/topic/addPic',
      filePath: this.data.tempFilePaths[i],
      name: 'file',
      formData:{
        topicid:this.data.topicid,
        index:i+1
      },
      success: function (res) {
      },
      complete: function(e){
        if(i==0){
          wx.showToast({
            title: '发表成功',
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/mine/topic/topic'
            })
          }, 2000) 
        }else{
          i--;
          that.uploadimg(i);
        }
      }
    })
  },

  checkPic:function(e){
    wx.uploadFile({
      url: 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=ACCESS_TOKEN',
      filePath: e,
      name:"file",
      header: {
        "Content-Type": "multipart/form-data"
      },
      success: function (res) {
        
      },
      fail: function (res) {
        wx.showModal({
          title: '错误提示',
          content: '上传图片失败',
          showCancel: false,
          success: function (res) { }
        })
      }
    })
  },




    
  
})